/**
 * 角色编辑、新增
 *
 * @author liuxiaotang
 */
define(["app", "js/services/MessageBox", "js/services/MuseService"], function (app) {

    var deps = ["$scope", "$state", "$modal", "$stateParams", "MessageBox", "MuseService"];

    function controller ($scope, $state, $modal, $stateParams, MessageBox, MuseService) {
        //获取数据
        $scope.getDetail = function () {
            MuseService.post("admin/adminauthres/requestmapping.json").success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.dataForTheTree = [];
                        var resultList = JSON.parse(data.data);
                        var tempList = [];
                        for (var x in resultList) {
                            var temp = {
                                "name": x,
                                "children": resultList[x]
                            };
                            tempList.push(temp);
                            $scope.expandedNodes.push(temp)
                        }
                        $scope.dataForTheTree = tempList;

                        //监听 （是否全选）
                        $scope.$watch('dataForTheTree', function () {
                            for (var i = 0; i < $scope.dataForTheTree.length; i++) {
                                if ($scope.dataForTheTree[i].children) {
                                    var checkedCount = 0;
                                    for (var j = 0; j < $scope.dataForTheTree[i].children.length; j++) {
                                        $scope.dataForTheTree[i].children[j].checked ? checkedCount++ : '';
                                    }
                                    $scope.dataForTheTree[i].checkedCount = checkedCount;
                                    if (checkedCount == 0) {
                                        $scope.dataForTheTree[i].checked = false;
                                    } else if (checkedCount == $scope.dataForTheTree[i].children.length) {
                                        $scope.dataForTheTree[i].checked = true;
                                    } else {
                                        $scope.dataForTheTree[i].checked = true;
                                    }
                                }
                            }
                        }, true);

                    } else {
                        MessageBox.notify('获取信息失败：' + data.message);
                    }
                });
        };
        $scope.getDetail();

        //保存
        $scope.saveObj = function () {
            //只传checked==true的
            var tempList = {};
            for (var i = 0; i < $scope.dataForTheTree.length; i++) {
                var children = {};
                var childrenList = {};
                children = $scope.dataForTheTree[i].children;
                for (var j = 0; j < children.length; j++) {
                    if (children[j].checked) {
                        childrenList[children[j].name] = children[j];
                    }
                }
                tempList[$scope.dataForTheTree[i].name] = childrenList;
            }
            console.log(tempList);
            console.log(JSON.stringify(tempList));
            MuseService.post("admin/adminauthres/import.json", {data: JSON.stringify(tempList)}).success(
                function (data, status, headers, config) {
                    if (data.state == 0) {
                        $scope.result = data.data;
                        MessageBox.notify('保存成功!');
                        $state.go('adminauthresList');
                    } else {
                        MessageBox.notify('保存失败：' + data.message);
                    }
                });
        };

        // 取消，返回
        $scope.close = function () {
            $state.go("adminauthresList");
        };

        $scope.treeOptions = {
            nodeChildren: "children",
            dirSelectable: false,
            injectClasses: {
                ul: "a1",//根目录
                li: "a2",//根节点
                liSelected: "a7",//根节点（选中）
                iExpanded: "a3",//根节点图标（被展开）
                iCollapsed: "a4",//根节点图标（被收起）
                iLeaf: "a5",//底层节点图标
                label: "a6",//节点文字
                labelSelected: "a8"//节点文字（选中）
            }
        };

        $scope.checkedNode = function (node) {
            if (node.children) {
                for (var i = 0; i < node.children.length; i++) {
                    node.children[i].checked = !node.checked;
                }
            }
        };
    }

    controller.$inject = deps;
    app.lazy.controller("AdminAuthResEditController", controller);
});
